package top.linuyx.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.linuyx.exception.GlobalException;
import top.linuyx.mapper.LabelMapper;
import top.linuyx.mapper.ShippingAddressMapper;
import top.linuyx.mapper.UserMapper;
import top.linuyx.pojo.param.ShippingAddressInsertParam;
import top.linuyx.pojo.param.ShippingAddressUpdateParam;
import top.linuyx.pojo.vo.ShippingAddressVO;
import top.linuyx.service.ShippingAddressService;
import top.linuyx.utils.MallUtils;

import java.util.List;

/**
 * @Author Linuyx
 * @Description ShippingAddress实现类 处理:查询收货地址、添加收货地址、修改收货地址、删除收货地址等业务
 * @Date Created in 2021-04-20 15:03
 */

@Service
public class ShippingAddressServiceImpl implements ShippingAddressService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LabelMapper labelMapper;

    @Autowired
    private ShippingAddressMapper shippingAddressMapper;

    /**
     * 根据用户名查询所有收货地址
     *
     * @return ShippingAddressVO
     */
    @Override
    public List<ShippingAddressVO> listAllShippingAddressByUsername() {
        //获取当前登录的用户
        String username = MallUtils.getUser();

        //根据用户名查询用户id
        Long userId = userMapper.getIdByUsername(username);
        List<ShippingAddressVO> shippingAddress= shippingAddressMapper.listAllShippingAddressById(userId);
        for (ShippingAddressVO address : shippingAddress) {
            address.setSexName(MallUtils.getSexName(address.getSexId()));
        }

        return shippingAddress;
    }

    /**
     * 添加收货地址
     *
     * @param insertParam ShippingAddress添加请求体
     */
    @Override
    public void insertAddress(ShippingAddressInsertParam insertParam) {
        //设置性别标识, 1 男, 0 女
        insertParam.setSexId(MallUtils.getSexId(insertParam.getSexName()));
        //设置默认地址标识, 1 是, 0 否
        insertParam.setIsDefault(MallUtils.getIsDefaultId(insertParam.getIsDefaultName()));

        //根据标签名称获取标签对应的id,并将labelId插入editParam
        Long labelId = labelMapper.getIdByname(insertParam.getLabelName());
        if (labelId == null) {
            throw new GlobalException("所选标签不存在");
        }
        insertParam.setLabelId(labelId);

        //获取当前登录的用户
        String userName = MallUtils.getUser();
        //根据用户名查询用户id
        Long userId = userMapper.getIdByUsername(userName);
        insertParam.setUserId(userId);

        //判断此地址是否设为默认地址
        if(insertParam.getIsDefault() == 1){
            //根据用户id查询默认收货地址id
            Long defaultShippingAddressId = shippingAddressMapper.getDefaultShippingAddressByUserId(userId);

            //判断之前是否有默认收货地址
            if(defaultShippingAddressId != null){
                //根据默认收货地址id取消默认收货地址
                shippingAddressMapper.updateDefaultShippingAddressByUserId(defaultShippingAddressId);
            }
        }

        //将收货地址插入到数据库
        shippingAddressMapper.insertAddress(insertParam);
    }

    /**
     * 根据id修改收货地址
     *
     * @param updateParam ShippingAddress修改请求体
     */
    @Override
    public void updateAddressById(ShippingAddressUpdateParam updateParam) {
        //设置性别标识, 1 男, 0 女
        updateParam.setSexId(MallUtils.getSexId(updateParam.getSexName()));
        //设置默认地址标识, 1 是, 0 否
        updateParam.setIsDefault(MallUtils.getIsDefaultId(updateParam.getIsDefaultName()));

        //根据标签名称获取标签对应的id,并将labelId插入editParam
        Long labelId = labelMapper.getIdByname(updateParam.getLabelName());
        if (labelId == null) {
            throw new GlobalException("所选标签不存在");
        }
        updateParam.setLabelId(labelId);

        //判断此地址是否设为默认地址
        if(updateParam.getIsDefault() == 1){
            //根据收货地址id查询用户id
            Long userId = shippingAddressMapper.getUserIdByShippingAddressId(updateParam.getShippingAddressId());

            //根据用户id查询默认收货地址id
            Long defaultShippingAddressId = shippingAddressMapper.getDefaultShippingAddressByUserId(userId);

            //判断之前是否有默认收货地址
            if(defaultShippingAddressId != null){
                //根据默认收货地址id取消默认收货地址
                shippingAddressMapper.updateDefaultShippingAddressByUserId(defaultShippingAddressId);
            }
        }

        shippingAddressMapper.updateAddressById(updateParam);
    }

    /**
     * 根据id删除收货地址
     *
     * @param shippingAddressId
     */
    @Override
    public void deleteAddressById(Long shippingAddressId) {
        shippingAddressMapper.deleteAddressById(shippingAddressId);
    }
}
